package com.itheima.controller;


import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptServiceImpl deptServiceImpl;

    //    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list(){
        System.out.println("查询全部部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);

    }
    @DeleteMapping
    public Result delete(@RequestParam(value = "id", required = false) Integer id){
//        String idStr = request.getParameter("id");
//        int id =  Integer.parseInt(idStr);
        System.out.println("根据ID删除部门数据:" + id);
        deptService.deleteDeptById(id);
        return Result.success();

    }

    @PostMapping
    public Result add(@RequestBody Dept dept){
        System.out.println("添加部门数据:" + dept);
        deptService.addDept(dept);
        return  Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        System.out.println("查询部门数据:" + id);
        Dept dept = deptService.getById(id);

        System.out.println("查询部门数据:" + dept);

        return Result.success(dept);
    }
    @PutMapping
    public Result update(@RequestBody Dept dept){
        System.out.println("修改部门数据:" + dept);
        deptService.updateDept(dept);
        return Result.success();
    }

}
